package com.origin.onlinesys.service.modules.user.impl;

import com.origin.onlinesys.common.services.BaseService;
import com.origin.onlinesys.domain.modules.interviewsys.Role;
import com.origin.onlinesys.domain.modules.interviewsys.RoleMenu;
import com.origin.onlinesys.persistence.modules.sys.account.RolesMapper;
import com.origin.onlinesys.service.modules.user.IRolesService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2015/3/3.
 */
@Service
public class RolesServiceImpl extends  BaseService<Role> implements IRolesService {

    @Override
    public Collection<Role> selectAllAsObj() {
        return null;
    }

    @Override
    public Collection<String> selectAll() {
        return null;
    }

    @Override
    public Collection<String> selectByUserId(String id) {
        return ((RolesMapper)(mapper)).selectByUserId(id);
    }

    @Override
    public Collection<Role> selectByUserIdAsObj(String id) {
        return null;
    }

    @Override
    public int addRolePermission(List<Map> list) {
        ((RolesMapper)mapper).addRolePermission(list);
        return 1;
    }

    @Override
    public List<Role> selectItem() {
        return ((RolesMapper)mapper).selectItem();
    }

    @Override
    public List<String> selectPIdByRId(String id) {
        return ((RolesMapper)mapper).selectPIdByRId(id);
    }

    @Override
    public int delRolePermission(String id) {
        ((RolesMapper)mapper).delRolePermission(id);
        return 1;
    }

    @Override
    public Role selectById(String id) {
        return ((RolesMapper)mapper).selectById(id);
    }

    @Override
    public List<RoleMenu> selectRoleMenu(String id) {
        List<RoleMenu> rml = new ArrayList<RoleMenu>();
        List<Map<String,Object>> list = ((RolesMapper)mapper).selectRoleMenu(id);

        Map<String,String> mMap = new HashMap<String,String>();
        for(Map<String,Object> map:list){
            if(mMap.get(map.get("pid"))==null && map.get("pid") != null){mMap.put(map.get("pid")+"","true");}
        }

        RoleMenu roleMenu=null;RoleMenu roleMenu1=null;
        List<RoleMenu> addList = null;
        for(String key:mMap.keySet()){
            roleMenu = new RoleMenu();addList = new ArrayList<RoleMenu>();
            for(Map<String,Object> map:list){
                if(Integer.parseInt(key)==Integer.parseInt(map.get("parent_id").toString())){
                    roleMenu1 = new RoleMenu();
                    roleMenu1.setTitle(getMapValue(map,"title"));
                    roleMenu1.setUrl(getMapValue(map,"url"));addList.add(roleMenu1);
                    roleMenu.setTitle(getMapValue(map,"ptitle"));
                    roleMenu.setUrl(getMapValue(map, "purl"));
                    roleMenu.setIcon(map.get("icon") != null ? map.get("icon").toString() : "fa-table");
                }
            }
            roleMenu.setList(addList);rml.add(roleMenu);
        }
        return rml;
    }

    @Override
    public Map selectRoleIdByUserId(String id) {
        return ((RolesMapper)mapper).selectRoleByUser(id).get(0);
    }

    private String getMapValue(Map map,String key){
        return map.get(key)!=null?map.get(key).toString():null;
    }
}
